#include <iostream>
#include <string>
#include "friend.pb.h"
#include "mprpcapplication.h"
#include "rpcprovider.h"
#include <vector>
class FriendService : public fixbug::FriendServiceRpc
{
public:
    std::vector<std::string> getFriendList(uint32_t userId)
    {
        std::cout << "doing getFriendList userid" << userId << std::endl;
        std::vector<std::string> vec;
        vec.push_back("tom");
        vec.push_back("full");
        vec.push_back("stack");
        return vec;
    }

    void GetFriendList(::google::protobuf::RpcController *controller,
                       const ::fixbug::GetFriendRequest *request,
                       ::fixbug::GetFriendResponse *response,
                       ::google::protobuf::Closure *done)
    {
        uint32_t userid = request->userid();
        std::vector<std::string> friendlists = getFriendList(userid);
        // 写回request
        response->mutable_result()->set_errcode(0);
        response->mutable_result()->set_errmsg("");
        for (std::string &name : friendlists)
        {
            std::string *p = response->add_friends();
            *p = name;
        }
        // std::cout << "getFriendList done size:" << response->friends_size() << std::endl;
        done->Run();
    }
};

int main(int argc, char *argv[])
{
    // 先调用框架的初始化工作,知道ip port // provider -i config.conf
    MprpcApplication::Init(argc, argv);

    // 使用muduo库
    RpcProvider provider;
    // 发布一个服务
    provider.NotifyService(new FriendService());
    // 启动服务 启动一个rpc服务发布节点 run以后，进程进入阻塞状态，等待远程得rpc调用请求
    provider.Run();

    return 0;
}